package com.glls.customer.controller;

import com.glls.common.entity.Goods;
import com.glls.customer.config.ConfigProperties;
import com.glls.customer.service.CustomerService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 在使用nacos 之前， 咱们要实现远程调用  ，  可以借助   restTemplate
 *
 * @author junyang
 * @version 1.0.0
 * @date 2025/2/19 10:29
 */
@RestController
@RequestMapping("/customer")
@Slf4j
//@RefreshScope
public class CustomerController {


    @Resource
    private RestTemplate restTemplate;

    @RequestMapping("/findGoodsById")
    public Goods findGoodsById(Integer id) {

        // 1. 获取服务地址
        String url = "http://localhost:9081/search/findGoodsById?id=" + id;
        // 2. 调用远程服务
        Goods goods = restTemplate.getForObject(url, Goods.class);

        return goods;

        //思考： 上面的写法有哪些弊端？
        //远程调用的地址发生变化了。 需要改代码
        //远程服务的地址 写死了
        //没办法实现负载均衡
        //等等
    }


    //使用 nacos 服务发现
    @Resource
    private DiscoveryClient discoveryClient;


    @RequestMapping("/findGoodsById2")
    public Goods findGoodsByIdWithNacos(Integer id) {
        // 借助nacos  动态的发现  远程服务的  ip  和 端口   ， 不用硬编码 写死
        List<String> services = discoveryClient.getServices();

        String host = "";
        int port = 0;

        for(String service : services){
            if(service.equals("service-search")){
                List<ServiceInstance> instances = discoveryClient.getInstances(service);

                host = instances.get(0).getHost();
                port = instances.get(0).getPort();

            }
        }

        // 这个地址  不是写死的
        String url = "http://"+host+":"+port+"/search/findGoodsById?id=" + id;

        log.info("远程调用search服务的url:{}",url);

        return restTemplate.getForObject(url, Goods.class);

    }





    @RequestMapping("/findGoodsById3")
    public Goods findGoodsByIdWithLoadBalance(Integer id) {
        // 借助nacos  动态的发现  远程服务的  ip  和 端口   ， 不用硬编码 写死

        // 这个地址  不是写死的
        String url = "http://service-search/search/findGoodsById?id="+id;

        log.info("远程调用search服务的url:{}",url);

        return restTemplate.getForObject(url, Goods.class);

    }
    @Resource
    private CustomerService customerService;
    @RequestMapping("/findGoodsById4")
    public Goods findGoodsByIdWithFeign(Integer id) {
        return customerService.findGoodsByIdWithFeign(id);
    }


    @GetMapping("/searchGoodsById")
    public Goods searchGoodsByIdWithFeign(Integer id,String name) {
        return customerService.searchGoodsByIdWithFeign(id,name);
    }


    @GetMapping("/findJifenById")
    public Goods findJifenByIdWithFeign(Integer id) {
        return customerService.findJifenByIdWithFeign(id);
    }

    @PostMapping("/save")
    public Map saveWithFeign(Goods goods) {
        return customerService.save(goods);
    }


    @PostMapping("/update")
    public Map updateWithFeign(Goods goods) {
        return customerService.update(goods);
    }


    @GetMapping("/deleteById")
    public Map deleteByIdWithFeign(Integer id) {
        return customerService.deleteById(id);
    }






    @Value("${customer.name}")
    private String name;

    @RequestMapping("/testConfig1")
    public String testConfig1(){
        return "testConfig1:" + name;
    }

    @Resource
    private ConfigProperties configProperties;

    @RequestMapping("/testConfig2")
    public String testConfig2(){
        return configProperties.getStuNo()+"-"+configProperties.getName()+"-"+configProperties.getSex();
    }


    @Value("${common.url}")
    private String url;
    @RequestMapping("/testConfig3")
    public String testConfig3(){
        //测试的是  命名空间 和 分组
        return url;
    }




}
